/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ASAInst;

import ASAExpr.ASAExpr;
import exception.DivisionByZeroException;
import exception.VariableNonDeclareeException;

/**
 *
 * @author FM & KB
 */
public class ASAInstFor extends ASAInst{

    ASAExpr initialisation;
    ASAExpr condition;
    ASAExpr incrementation;
    ASAInst instructions;
    
    public ASAInstFor(ASAExpr initialisation, ASAExpr condition, ASAExpr incrementation, ASAInst instructions){
        this.initialisation = initialisation;
        this.incrementation = incrementation;
        this.condition      = condition;
        this.instructions   = instructions;
    }
    
    @Override
    public void executer() throws VariableNonDeclareeException, DivisionByZeroException{
        for(initialisation.evalue();condition.evalue() != 0; incrementation.evalue()){
            instructions.executer();
        }
    }

    @Override
    public String notationExplorateur(String prefixe) {
        String res = prefixe + getLabelExplorateur() + "\n";
        String prefixeSuiv = prefixe + "   ";

        if (this.initialisation != null && this.condition != null && this.incrementation != null) {
            res += this.initialisation.notationExplorateur(prefixeSuiv);
            res += this.condition.notationExplorateur(prefixeSuiv);
            res += this.incrementation.notationExplorateur(prefixeSuiv);
            if(this.instructions != null){
                res += this.instructions.notationExplorateur(prefixeSuiv);
            }
        } else {
            res += prefixeSuiv + "VIDE\n";
        }
        return res;
    }
    
    @Override
    public String getLabelExplorateur() {
        return "FOR";
    }
    
}
